﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using DotNetCore上传.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using System.IO;

namespace DotNetCore上传.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IWebHostEnvironment _env;

        public HomeController(ILogger<HomeController> logger, IWebHostEnvironment env)
        {
            _logger = logger;
            _env = env;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 表单上传
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        public async Task<IActionResult> FileSave(List<IFormFile> files)
        {
            ///var files = Request.Form.Files;
            long size = files.Sum(f => f.Length);
            string webRootPath = _env.WebRootPath;

            string contentRootPath = _env.ContentRootPath;

            foreach (var formFile in files)

            {

                if (formFile.Length > 0)

                {
                    string fileExt = Path.GetExtension(formFile.FileName); //文件扩展名，不含“.”

                    long fileSize = formFile.Length; //获得文件大小，以字节为单位

                    string newFileName = System.Guid.NewGuid().ToString() + "." + fileExt; //随机生成新的文件名

                    var filePath = webRootPath + "/upload/" + newFileName;

                    using (var stream = new FileStream(filePath, FileMode.Create))

                    {

                        await formFile.CopyToAsync(stream);

                    }

                }

            }
            return Ok(new { count = files.Count, size });

        }


        /// <summary>
        /// ajax上传
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> FileSaveAjax()
        {
            var date = Request;
            var files = Request.Form.Files;
            long size = files.Sum(f => f.Length);
            string webRootPath = _env.WebRootPath;
            string contentRootPath = _env.ContentRootPath;
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    string fileExt = Path.GetExtension(formFile.FileName); //文件扩展名，不含“.”
                    long fileSize = formFile.Length; //获得文件大小，以字节为单位

                    string newFileName = System.Guid.NewGuid().ToString() + "." + fileExt; //随机生成新的文件名
                    var filePath = webRootPath + "/upload/" + newFileName;
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);
                    }
                }
            }
            return Ok(new { count = files.Count, size });
        }


    }
}
